﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CP.Utility
{
    public class QueryCondition
    {
        Hashtable formats = new Hashtable();
        Hashtable fields = new Hashtable();
        //link programm
        public QueryCondition Add(string fieldname, string value)
        {
            if (string.IsNullOrEmpty(value))
                return this;
            fields.Add(fieldname, value);
            return this;
        }
        public QueryCondition AddFormat(string formatstring, string value)
        {
            formats.Add(formatstring, value);
            return this;
        }

        public string BuildConditionString()
        {
            //if datetime or int not string, how to format to be continued
            StringBuilder sb = new StringBuilder();
            foreach (object key in fields.Keys)
            {
                sb.Append(" and ");
                sb.Append(key.ToString());
                sb.Append(" = '");
                sb.Append(fields[key]);
                sb.Append("' ");
            }
            foreach (object key in formats.Keys)
            {
                sb.Append(string.Format(key.ToString(), formats[key].ToString()));
            }

            return sb.ToString();
        }
    }
}
